package cn.yunyichina.reservation.component;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by Lullaby on 2016/8/20.
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private volatile static SpringContextUtil instance;

    private SpringContextUtil() {

    }

    public static SpringContextUtil getInstance() {
        if (instance == null) {
            synchronized (SpringContextUtil.class) {
                if (instance == null) {
                    instance = new SpringContextUtil();
                }
            }
        }
        return instance;
    }

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    private ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <FUCKGFW> FUCKGFW getBean(String name) {
        return (FUCKGFW) SpringContextUtil.getInstance().getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return SpringContextUtil.getInstance().getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return SpringContextUtil.getInstance().getApplicationContext().getBean(name, clazz);
    }

}
